/**
 * <p>Copyright: Copyright (c) 2012</p>
 * <p>Company: 联动优势科技有限公司</p>
 * <p>May 2, 20134:33:53 PM</p>
 * @author Ding Zhe
 * @version 1.0
 */
package com.umpay.privi.ejb.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.umpay.common.persist.ibatis.DataAccessException;
import com.umpay.privi.common.BeanManager;
import com.umpay.privi.common.Constants;
import com.umpay.privi.common.ReqMessage;
import com.umpay.privi.common.ResMessage;
import com.umpay.privi.common.RetCode;
import com.umpay.privi.common.XmlData;
import com.umpay.privi.dao.on.sys.PrivilegeDao;
import com.umpay.privi.dao.on.sys.PrivilegeMenuDao;
import com.umpay.privi.dao.on.sys.PrivilegeResDao;
import com.umpay.privi.exception.BusinessException;
import com.umpay.privi.helper.ReqParaCheckHelper;
import com.umpay.privi.model.sys.Privilege;
import com.umpay.privi.model.sys.PrivilegeMenu;
import com.umpay.privi.model.sys.PrivilegeRes;
import com.umpay.privi.service.sys.PrivilegeServiceRemote;
import com.umpay.privi.util.ListUtil;
import com.umpay.privi.util.SeqUtil;
import com.umpay.privi.util.StringUtil;

/** 
 * desc:权限EJB
 * <p>创建人：Ding Zhe 创建日期：May 2, 2013 </p>
 * @version V1.0  
 */

@TransactionManagement(TransactionManagementType.BEAN)
public class PrivilegeServiceBean implements PrivilegeServiceRemote{
	private static Logger logger = LoggerFactory.getLogger(PrivilegeServiceBean.class); 
	PrivilegeDao privilegeDao = BeanManager.getBean("privilegeDao", PrivilegeDao.class);
	PrivilegeResDao privilegeResDao = BeanManager.getBean("privilegeResDao", PrivilegeResDao.class);
	PrivilegeMenuDao privilegeMenuDao = BeanManager.getBean("privilegeMenuDao", PrivilegeMenuDao.class);
	/* 
	 * desc:查询所有系统权限的具体实现方法
	 * (non-Javadoc)
	 * @see com.umpay.privi.service.sys.PrivilegeServiceRemote#queryAllSysRrivileges(com.umpay.privi.common.ReqMessage)
	 */
	public ResMessage queryAllSysPrivileges(ReqMessage req) {
		//创建报文返回对象
		ResMessage res = new ResMessage();
		try {
			logger.info(String.format("[查询所有系统权限]查询请求参数：", req.getReqMap()));
			//上送数据校验
			ResMessage checkPara = ReqParaCheckHelper.checkRequire(req, "PrivilegeServiceBean.queryAllSysRrivileges.require");
			if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
				return checkPara;
			}
			List<Privilege> privilegeList = privilegeDao.findList(req.getReqMap());
			res.put(XmlData.RESULT_LIST, privilegeList);
			res.setRetCode(RetCode.SUCCESS);
			res.setRetMsg("成功");
		}  catch (DataAccessException e) {
			logger.error("[查询所有系统权限]异常：数据错误!");
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据错误");
		} catch (Exception e) {
			logger.error("[查询所有系统权限]未知异常",e);
			res.setRetCode(RetCode.UNKNOWN);
			res.setRetMsg("未知异常");
		}
		return res;
	}

	/* 
	 * desc:根据父权限Id获取该权限的下一级别的所有权限
	 * (non-Javadoc)
	 * @see com.umpay.privi.service.sys.PrivilegeServiceRemote#queryNextLevelRrivileges(com.umpay.privi.common.ReqMessage)
	 */
	public ResMessage queryNextLevelPrivileges(ReqMessage req) {
		//创建报文返回对象
		ResMessage res = new ResMessage();
		try {
			logger.info(String.format("[查询下级系统权限]查询请求参数：", req.getReqMap()));
			//上送数据校验
			ResMessage checkPara = ReqParaCheckHelper.checkRequire(req, "PrivilegeServiceBean.queryNextLevelRrivileges.require");
			if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
				return checkPara;
			}
			List<Privilege> privilegeList = privilegeDao.findList(req.getReqMap());
			res.put(XmlData.RESULT_LIST, privilegeList);
			res.setRetCode(RetCode.SUCCESS);
			res.setRetMsg("成功");
		}  catch (DataAccessException e) {
			logger.error("[查询下级系统权限]异常：数据错误!");
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据错误");
		} catch (Exception e) {
			logger.error("[查询下级系统权限]未知异常",e);
			res.setRetCode(RetCode.UNKNOWN);
			res.setRetMsg("未知异常");
		}
		return res;
	}

	/* 
	 * desc:根据角色号获取该角色所有的权限
	 * (non-Javadoc)
	 * @see com.umpay.privi.service.sys.PrivilegeServiceRemote#queryRrivilegesByRoleId(com.umpay.privi.common.ReqMessage)
	 */
	public ResMessage queryPrivilegesByRoleId(ReqMessage req) {
		//创建报文返回对象
		ResMessage res = new ResMessage();
		try {
			logger.info(String.format("[根据角色号查询系统权限]查询请求参数：", req.getReqMap()));
			//上送数据校验
			ResMessage checkPara = ReqParaCheckHelper.checkRequire(req, "PrivilegeServiceBean.queryRrivilegesByRoleId.require");
			if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
				return checkPara;
			}
			List<Privilege> privilegeList = privilegeDao.findListByRoleId(req.getReqMap());
			res.put(XmlData.RESULT_LIST, privilegeList);
			res.setRetCode(RetCode.SUCCESS);
			res.setRetMsg("成功");
		}  catch (DataAccessException e) {
			logger.error("[根据角色号查询系统权限]异常：数据错误!");
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据错误");
		} catch (Exception e) {
			logger.error("[根据角色号查询系统权限]未知异常",e);
			res.setRetCode(RetCode.UNKNOWN);
			res.setRetMsg("未知异常");
		}
		return res;
	}
	/**
	 * 
	 * desc:通过多个角色Id查询权限列表
	 * <p>创建人：liuguangjun , May 15, 2013 3:19:36 PM</p>
	 * @param qryMap
	 * @return
	 * @throws DataAccessException
	 */
	@SuppressWarnings("unchecked")
	public ResMessage queryPrivilegesByRoleIdList(ReqMessage req) {
		logger.info("[根据多个角色号查询系统权限]查询请求参数："+ req.getReqMap());
		//上送数据校验
		ResMessage checkPara = ReqParaCheckHelper.checkRequire(req, "PrivilegeServiceBean.queryRrivilegesByRoleIdList.require");
		if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
			return checkPara;
		}
		//创建报文返回对象
		ResMessage res = new ResMessage();
		try {
			List<String> roleList=(List<String>)req.get(XmlData.ROLEID_LIST);
			if(roleList==null||roleList.size()==0){
				throw new BusinessException(RetCode.REQPARAM_ERR,"roleIdList不能为空");
			}
			req.put(XmlData.ROLEID_LIST,ListUtil.joinAs(roleList,","));
			List<Privilege> privilegeList = privilegeDao.findListByRoleIdList(req.getReqMap());
			res.put(XmlData.RESULT_LIST, privilegeList);
			res.setRetCode(RetCode.SUCCESS);
			res.setRetMsg("成功");
		}catch (BusinessException e) {
			logger.error("请求数据不正确",e);
			res.setRetCode(e.getCode());
			res.setRetMsg(e.getMessage());
		}catch (DataAccessException e) {
			logger.error("[根据多个角色号查询系统权限]异常：数据错误!",e);
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据错误");
		}catch (Exception e) {
			logger.error("[根据多个角色号查询系统权限]未知异常",e);
			res.setRetCode(RetCode.UNKNOWN);
			res.setRetMsg("未知异常");
		}
		return res;
	}

	public ResMessage addPrivilege(ReqMessage req) {
		logger.info("[添加系统权限]查询请求参数："+ req.getReqMap());
		//上送数据校验
		ResMessage checkPara = ReqParaCheckHelper.checkRequire(req, "PrivilegeServiceBean.addPrivilege.require");
		if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
			return checkPara;
		}
		//创建报文返回对象
		ResMessage res = new ResMessage();
		try {
			Privilege privilege = new Privilege();
			int privilegeId = SeqUtil.genSequence(Constants.PRIVILEGE_SEQ)+1000;
			privilege.setPrivilegeId(privilegeId);
			privilege.setPrivilegeName(StringUtil.trim(req.getString(XmlData.PRIVILEGE_NAME)));
			privilege.setPrivilegeType(Short.parseShort(req.getString(XmlData.PRIVILEGE_TYPE)));
			privilege.setParentId(Integer.parseInt(req.getString(XmlData.PARENTID)));
			privilege.setIsLeaf(Short.parseShort(req.getString(XmlData.IS_LEAF)));
			privilege.setSysId(Short.parseShort(req.getString(XmlData.SYSID)));
			privilege.setDescription(req.getString(XmlData.DESCRIPTION));
			privilege.setCreator(req.getString(XmlData.CREATOR));
			if(StringUtil.isNotEmpty(req.getString(XmlData.DEEP)))privilege.setDeep(Short.parseShort(req.getString(XmlData.DEEP)));
			if(StringUtil.isNotEmpty(req.getString(XmlData.SORT)))privilege.setSort(Short.parseShort(req.getString(XmlData.SORT)));
			if(StringUtil.isNotEmpty(req.getString(XmlData.FLAG)))privilege.setFlag(Short.parseShort(req.getString(XmlData.FLAG)));
			privilegeDao.insert(privilege);
			res.setRetCode(RetCode.SUCCESS);
			res.setRetMsg("成功");
		}catch (DataAccessException e) {
			logger.error("[添加系统权限]异常：数据错误!",e);
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据错误");
		}catch (Exception e) {
			logger.error("[添加系统权限]未知异常",e);
			res.setRetCode(RetCode.UNKNOWN);
			res.setRetMsg("未知异常");
		}
		return res;
	}

	public ResMessage modifyPrivilege(ReqMessage req) {
		logger.info("[修改系统权限]查询请求参数："+ req.getReqMap());
		//上送数据校验
		ResMessage checkPara = ReqParaCheckHelper.checkRequire(req, "PrivilegeServiceBean.modifyPrivilege.require");
		if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
			return checkPara;
		}
		//创建报文返回对象
		ResMessage res = new ResMessage();
		try {
			Privilege privilege = new Privilege();
			privilege.setPrivilegeId(Integer.parseInt(req.getString(XmlData.PRIVILEGEID)));
			privilege.setPrivilegeName(StringUtil.trim(req.getString(XmlData.PRIVILEGE_NAME)));
			if(StringUtil.isNotEmpty(req.getString(XmlData.PRIVILEGE_TYPE)))privilege.setPrivilegeType(Short.parseShort(req.getString(XmlData.PRIVILEGE_TYPE)));
			if(StringUtil.isNotEmpty(req.getString(XmlData.PARENTID)))privilege.setParentId(Integer.parseInt(req.getString(XmlData.PARENTID)));
			if(StringUtil.isNotEmpty(req.getString(XmlData.IS_LEAF)))privilege.setIsLeaf(Short.parseShort(req.getString(XmlData.IS_LEAF)));
			if(StringUtil.isNotEmpty(req.getString(XmlData.SYSID)))privilege.setSysId(Short.parseShort(req.getString(XmlData.SYSID)));
			privilege.setDescription(req.getString(XmlData.DESCRIPTION));
			privilege.setModUser(req.getString(XmlData.MODUSER));
			if(StringUtil.isNotEmpty(req.getString(XmlData.DEEP)))privilege.setDeep(Short.parseShort(req.getString(XmlData.DEEP)));
			if(StringUtil.isNotEmpty(req.getString(XmlData.SORT)))privilege.setSort(Short.parseShort(req.getString(XmlData.SORT)));
			if(StringUtil.isNotEmpty(req.getString(XmlData.FLAG)))privilege.setFlag(Short.parseShort(req.getString(XmlData.FLAG)));
			privilegeDao.update(privilege);
			res.setRetCode(RetCode.SUCCESS);
			res.setRetMsg("成功");
		}catch (DataAccessException e) {
			logger.error("[修改系统权限]异常：数据错误!",e);
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据错误");
		}catch (Exception e) {
			logger.error("[修改系统权限]未知异常",e);
			res.setRetCode(RetCode.UNKNOWN);
			res.setRetMsg("未知异常");
		}
		return res;
	}

	public ResMessage queryPrivilegeMenu(ReqMessage req) {
		logger.info("[根据权限ID查询已开通的菜单]查询请求参数："+ req.getReqMap());
		//上送数据校验
		ResMessage checkPara = ReqParaCheckHelper.checkRequire(req, "PrivilegeServiceBean.queryPrivilegeMenu.require");
		if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
			return checkPara;
		}
		//创建报文返回对象
		ResMessage res = new ResMessage();
		try {
			List<PrivilegeMenu> privilegeList = privilegeMenuDao.findList(req.getReqMap());
			res.put(XmlData.RESULT_LIST, privilegeList);
			res.setRetCode(RetCode.SUCCESS);
			res.setRetMsg("成功");
		}catch (DataAccessException e) {
			logger.error("[根据多个角色号查询系统权限]异常：数据错误!",e);
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据错误");
		}catch (Exception e) {
			logger.error("[根据多个角色号查询系统权限]未知异常",e);
			res.setRetCode(RetCode.UNKNOWN);
			res.setRetMsg("未知异常");
		}
		return res;
	}

	public ResMessage queryPrivilegeRes(ReqMessage req) {
		logger.info("[根据权限ID查询已开通的资源]查询请求参数："+ req.getReqMap());
		//上送数据校验
		ResMessage checkPara = ReqParaCheckHelper.checkRequire(req, "PrivilegeServiceBean.queryPrivilegeRes.require");
		if (!RetCode.SUCCESS.equals(checkPara.getRetCode())) {
			return checkPara;
		}
		//创建报文返回对象
		ResMessage res = new ResMessage();
		try {
			List<PrivilegeRes> privilegeList = privilegeResDao.findList(req.getReqMap());
			res.put(XmlData.RESULT_LIST, privilegeList);
			res.setRetCode(RetCode.SUCCESS);
			res.setRetMsg("成功");
		}catch (DataAccessException e) {
			logger.error("[根据权限ID查询已开通的资源]异常：数据错误!",e);
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据错误");
		}catch (Exception e) {
			logger.error("[根据权限ID查询已开通的资源]未知异常",e);
			res.setRetCode(RetCode.UNKNOWN);
			res.setRetMsg("未知异常");
		}
		return res;
	}

	@SuppressWarnings("unchecked")
	public ResMessage addPrivilegeMenu(ReqMessage req) {
		logger.info("[添加权限菜单]查询请求参数："+ req.getReqMap());
		//创建报文返回对象
		ResMessage res = new ResMessage();
		try {
			List<PrivilegeMenu> list=(List<PrivilegeMenu>)req.get(XmlData.MENU_LIST);
			if(list==null||list.size()==0){
				throw new BusinessException(RetCode.REQPARAM_ERR,"list不能为空");
			}
			Map<String,Object> map = new HashMap<String,Object>();
			
			map.put(XmlData.PRIVILEGEID, req.getString(XmlData.PRIVILEGEID));
			map.put(XmlData.FLAG, "1");
			
			privilegeMenuDao.updatePrivilegeMenu(map);
			
			privilegeMenuDao.insertBatch(list);
			
			res.setRetCode(RetCode.SUCCESS);
			res.setRetMsg("成功");
		}catch (BusinessException e) {
			logger.error("请求数据不正确",e);
			res.setRetCode(e.getCode());
			res.setRetMsg(e.getMessage());
		}catch (DataAccessException e) {
			logger.error("[添加权限菜单]异常：数据错误!",e);
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据错误");
		}catch (Exception e) {
			logger.error("[添加权限菜单]未知异常",e);
			res.setRetCode(RetCode.UNKNOWN);
			res.setRetMsg("未知异常");
		}
		return res;
	}

	@SuppressWarnings("unchecked")
	public ResMessage addPrivilegeRes(ReqMessage req) {
		logger.info("[添加权限资源]查询请求参数："+ req.getReqMap());
		//创建报文返回对象
		ResMessage res = new ResMessage();
		try {
			List<PrivilegeRes> list=(List<PrivilegeRes>)req.get(XmlData.RES_LIST);
			Map<String,Object> map = new HashMap<String,Object>();
			
			map.put(XmlData.PRIVILEGEID, req.getString(XmlData.PRIVILEGEID));
			map.put(XmlData.FLAG, "1");
			
			privilegeResDao.updatePrivilegeRes(map);
			
			privilegeResDao.insertBatch(list);
			res.setRetCode(RetCode.SUCCESS);
			res.setRetMsg("成功");
		}catch (DataAccessException e) {
			logger.error("[添加权限资源]异常：数据错误!",e);
			res.setRetCode(RetCode.DB_ERROR);
			res.setRetMsg("数据错误");
		}catch (Exception e) {
			logger.error("[添加权限资源]未知异常",e);
			res.setRetCode(RetCode.UNKNOWN);
			res.setRetMsg("未知异常");
		}
		return res;
	}
}
